#!/bin/sh

set -e

CLOJURE_RELEASE="1.9.0"
SPEC_ALPHA_RELEASE="0.1.143"
CORE_SPECS_ALPHA_RELEASE="0.1.24"
CLOSURE_RELEASE="20250820"
GCLOSURE_LIB_RELEASE="0.0-20250515-f04e4c0e"
TREADER_RELEASE="1.3.6"
TEST_CHECK_RELEASE="1.1.1"

# check dependencies
curl -V >/dev/null  || { echo "cURL is missing, or not on your system path."; exit 1; }
unzip -v >/dev/null || { echo "The 'unzip' utility is missing, or not on your system path."; exit 1; }

rm -rf lib

mkdir -p lib

echo "Fetching Clojure..."
curl -O -s https://repo1.maven.org/maven2/org/clojure/clojure/$CLOJURE_RELEASE/clojure-$CLOJURE_RELEASE.jar || { echo "Download failed."; exit 1; }
echo "Copying clojure-$CLOJURE_RELEASE.jar to lib/clojure.jar..."
cp clojure-$CLOJURE_RELEASE.jar lib/clojure.jar
echo "Cleaning up Clojure jar..."
rm clojure-$CLOJURE_RELEASE.jar

echo "Fetching specs.alpha...."
curl --retry 3 -O -s https://repo1.maven.org/maven2/org/clojure/spec.alpha/$SPEC_ALPHA_RELEASE/spec.alpha-$SPEC_ALPHA_RELEASE.jar || { echo "Download failed."; exit 1; }
echo "Copying spec.alpha-$SPEC_ALPHA_RELEASE/spec.alpha-$SPEC_ALPHA_RELEASE.jar to lib/spec.alpha.jar..."
cp spec.alpha-$SPEC_ALPHA_RELEASE.jar lib/spec.alpha-$SPEC_ALPHA_RELEASE.jar
echo "Cleaning up spec.alpha..."
rm spec.alpha-$SPEC_ALPHA_RELEASE.jar

echo "Fetching core.specs.alpha...."
curl --retry 3 -O -s https://repo1.maven.org/maven2/org/clojure/core.specs.alpha/$CORE_SPECS_ALPHA_RELEASE/core.specs.alpha-$CORE_SPECS_ALPHA_RELEASE.jar || { echo "Download failed."; exit 1; }
echo "Copying core.specs.alpha-$CORE_SPECS_ALPHA_RELEASE/core.specs.alpha-$CORE_SPECS_ALPHA_RELEASE.jar to lib/core.specs.alpha.jar..."
cp core.specs.alpha-$CORE_SPECS_ALPHA_RELEASE.jar lib/core.specs.alpha-$CORE_SPECS_ALPHA_RELEASE.jar
echo "Cleaning up core.specs.alpha..."
rm core.specs.alpha-$CORE_SPECS_ALPHA_RELEASE.jar

echo "Fetching Google Closure library..."
mkdir -p closure/library
cd closure/library
if [ "$1" = "--closure-library-head" ] ; then
    echo "Building against HEAD of Google Closure library..."

    # Existing checkout?
    if [ -d closure-library ] ; then
        echo "Updating Google Closure library from HEAD..."
        cd closure-library
        git pull
        cd ..
    else
        echo "Checking out HEAD of Google Closure library..."
        rm -rf *
        git clone https://github.com/google/closure-library
    fi
else
    curl --retry 3 -O -s https://repo1.maven.org/maven2/org/clojure/google-closure-library/$GCLOSURE_LIB_RELEASE/google-closure-library-$GCLOSURE_LIB_RELEASE.jar || { echo "Download failed."; exit 1; }
    cp google-closure-library-$GCLOSURE_LIB_RELEASE.jar ../../lib/google-closure-library-$GCLOSURE_LIB_RELEASE.jar
    rm google-closure-library-$GCLOSURE_LIB_RELEASE.jar

    echo "Fetching Google Closure third party library..."
    curl --retry 3 -O -s https://repo1.maven.org/maven2/org/clojure/google-closure-library-third-party/$GCLOSURE_LIB_RELEASE/google-closure-library-third-party-$GCLOSURE_LIB_RELEASE.jar || { echo "Download failed."; exit 1; }
    cp google-closure-library-third-party-$GCLOSURE_LIB_RELEASE.jar ../../lib/google-closure-library-third-party-$GCLOSURE_LIB_RELEASE.jar
    rm google-closure-library-third-party-$GCLOSURE_LIB_RELEASE.jar
fi
cd ../..

echo "Fetching Google Closure compiler..."
if [ "$1" = "--closure-compiler-snapshot" ] ; then
    curl --retry 3 -O -s https://oss.sonatype.org/content/repositories/snapshots/com/google/javascript/closure-compiler/1.0-SNAPSHOT/maven-metadata.xml || { echo "Download failed."; exit 1; }
    CC_JAR_VERSION=`grep value maven-metadata.xml | head -1 | awk -F '[<>]' '/value/{print $3}'`
    CC_JAR_NAME=closure-compiler-$CC_JAR_VERSION.jar
    curl --retry 3 -O -s https://oss.sonatype.org/content/repositories/snapshots/com/google/javascript/closure-compiler/1.0-SNAPSHOT/$CC_JAR_NAME || { echo "Download failed."; exit 1; }
    cp $CC_JAR_NAME lib/$CC_JAR_NAME
    echo "Cleaning up closure-compiler.jar..."
    rm $CC_JAR_NAME
    rm maven-metadata.xml
else
    curl --retry 3 -O -s https://repo1.maven.org/maven2/com/google/javascript/closure-compiler/v$CLOSURE_RELEASE/closure-compiler-v$CLOSURE_RELEASE.jar || { echo "Download failed."; exit 1; }
    cp closure-compiler-v$CLOSURE_RELEASE.jar lib/closure-compiler-v$CLOSURE_RELEASE.jar
    echo "Cleaning up closure-compiler.jar..."
    rm closure-compiler-v$CLOSURE_RELEASE.jar
fi

if [ "$1" = "--closure-library-head" ] ; then
    echo "Building lib/google-closure-library-HEAD.jar..."
    # Set up the third-party deps paths to match what we would get when depending on a release
    sed -e 's/..\/..\/third_party\/closure\/goog\///' closure/library/closure-library/closure/goog/deps.js > revised-deps.js
    cp closure/library/closure-library/closure/goog/deps.js orig-deps.js
    mv revised-deps.js closure/library/closure-library/closure/goog/deps.js
    jar cf ./lib/google-closure-library-HEAD.jar -C closure/library/closure-library/closure/ goog
    mv orig-deps.js closure/library/closure-library/closure/goog/deps.js
    echo "Building lib/google-closure-library-third-party-HEAD.jar..."
    mv closure/library/closure-library/third_party/closure/goog/deps.js orig-deps.js
    mv closure/library/closure-library/third_party/closure/goog/base.js orig-base.js
    jar cf ./lib/google-closure-library-third-party-HEAD.jar -C closure/library/closure-library/third_party/closure/ goog
    mv orig-base.js closure/library/closure-library/third_party/closure/goog/base.js
    mv orig-deps.js closure/library/closure-library/third_party/closure/goog/deps.js
fi

echo "Fetching tools.reader $TREADER_RELEASE ..."
curl --retry 3 -O -s https://repo1.maven.org/maven2/org/clojure/tools.reader/$TREADER_RELEASE/tools.reader-$TREADER_RELEASE.jar || { echo "Download failed."; exit 1; }

echo "Moving tools.reader.jar to lib/tools.reader.jar"
mv tools.reader-$TREADER_RELEASE.jar lib/tools.reader-$TREADER_RELEASE.jar

echo "Fetching test.check $TEST_CHECK_RELEASE ..."
curl --retry 3 -O -s https://repo1.maven.org/maven2/org/clojure/test.check/$TEST_CHECK_RELEASE/test.check-$TEST_CHECK_RELEASE.jar || { echo "Download failed."; exit 1; }

echo "Moving test.check.jar to lib/test.check.jar"
mv test.check-$TEST_CHECK_RELEASE.jar lib/test.check.jar

echo "[Bootstrap Completed]"
